﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Microsoft.Expression.Interactivity.Core;
using System.Windows.Input;
using PropertyTools.Wpf;
using InteControlSys.VehicleControlService;

namespace InteControlSys.ViewModel
{
    public class ArriveNodeViewModel : NodeViewModel
    {
        public ArriveNodeViewModel(string name, MainWindowViewModel root, int truck, int statusId)
        {
            this.Name = name;
            this.Parent = null;
            this.RootVM = root;
            this.IsExpanded = true;
            this.TruckCount = truck;
            this.StatusID = statusId;
            this.NodeType = "ArriveNodeViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();
        }       
        public override Uri GetMyIcon()
        {
            return null;
        }
        public override void ExecuteConfirmCommand(object parameter)
        {
            List<NodeViewModel> selItems = OperatingTruckList.Where(x => x.IsSelected).ToList();

        }

        #region Property
        private List<CameraInfo> camera;
        public List<CameraInfo> Camera
        {
            get { return camera; }
            set
            {
                camera = value;
                NotifyChanged("Camera");
            }
        }

        private CameraInfo _selectedCamera;
        public CameraInfo SelectedCamera
        {
            get { return _selectedCamera; }
            set
            {
                _selectedCamera = value;
                NotifyChanged("SelectedCamera");
            }
        }

        private string newConveyanceNo;
        public string NewConveyanceNo
        {
            get { return newConveyanceNo; }
            set
            {
                newConveyanceNo = value;
                NotifyChanged("NewConveyanceNo");
                newTruckList = new List<TransportInfo>();
                this.NewTruckList = newTruckList;
            }
        }

        private string _warningMSG = null;
        public string WarningMSG
        {
            get { return _warningMSG; }
            set
            {
                _warningMSG = value;
                NotifyChanged("WarningMSG");
            }
        }

        private List<TransportInfo> newTruckList;
        public List<TransportInfo> NewTruckList
        {
            get { return newTruckList; }
            set
            {
                newTruckList = value;
                NotifyChanged("NewTruckList");
            }
        }

        private TransportInfo _selectedNew;
        public TransportInfo SelectedNew
        {
            get { return _selectedNew; }
            set
            {
                _selectedNew = value;
                NotifyChanged("SelectedNew");
            }
        }

        private string remark = null;
        public string Remark
        {
            get { return remark; }
            set
            {
                remark = value;
                NotifyChanged("Remark");
            }
        }

        #endregion

        #region Command
        public ICommand CreateWorkflow
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    camera = MainWindowViewModel.ControlService.ListCandidateCameraInfo(1).ToList();
                    this.Camera = camera;
                    Remark = remark;
                    WarningMSG = _warningMSG;
                    RootVM.IsEditWindowVisible = true;
                });
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    RootVM.IsEditWindowVisible = false;
                });
            }
        }

        public ICommand GetShipDoc
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    this.WarningMSG = null;
                    newTruckList = new List<TransportInfo>();
                    if (SelectedCamera == null)
                    {
                        this.WarningMSG = "请选择车辆入厂摄像头";
                       
                    }
                    else if (NewConveyanceNo == null || NewConveyanceNo.Trim() == string.Empty || NewConveyanceNo.Length < 7)
                    {
                        this.WarningMSG = "请填写正确的车牌号";
                    }
                    else
                    {
                        newTruckList = MainWindowViewModel.ControlService.GetTransportInfo(NewConveyanceNo, 0).ToList();
                        if (newTruckList.Count == 0)
                        {
                            this.WarningMSG = "没有找到对应的运输单据！请检查输入的车牌号是否正确。";
                        }
                        
                    }
                    this.NewTruckList = newTruckList;

                });
            }
        }

        public ICommand ConfirmCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    this.WarningMSG = null;
                    
                    if (SelectedCamera == null)
                    {
                        this.WarningMSG = "请选择车辆入厂摄像头";
                    }
                    else if (NewConveyanceNo == null || NewConveyanceNo.Trim() == string.Empty || NewConveyanceNo.Length < 7)
                    {
                        this.WarningMSG = "请填写正确的车牌号";
                    }
                    else if (SelectedNew == null)
                    {
                        this.WarningMSG = "未选择关联的运输单据。";
                    }
                    else
                    {
                        RootVM.IsEditWindowVisible = false;
                        RootVM.IsLoading = true;
                        var cameraInfo = new CameraMessageInfo();

                        cameraInfo.CameraID = SelectedCamera.ID;
                        cameraInfo.ConveyanceNo = NewConveyanceNo;
                        cameraInfo.ActionTime = DateTime.Now;
                        cameraInfo.ID = SelectedNew.ID;
                        cameraInfo.NodeID = SelectedCamera.Node.ID;
                        cameraInfo.OperatorNo = MainWindow.OperateNo;
                        cameraInfo.Remark = this.Remark;

                        var result=MainWindowViewModel.ControlService.CreateWorkflow(cameraInfo);

                        if (result.Success)
                        {
                            this.WarningMSG = "已成功启动流程。";
                        }
                        else
                        {
                            this.WarningMSG = result.ErrorMessage;
                        }

                        RootVM.IsLoading = false;
                        RootVM.IsEditWindowVisible = true;

                    }
                });
            }
        }

        #endregion
    }
}
